package service;

import mapper.BrandMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import pagination.PageBean;
import pojo.Brand;
import utils.SqlSessionFactoryUtils;
import java.util.List;



public class BrandService {

    //分页查询
    public PageBean<Brand> selectAllPage(int currentPage, int pageSize) {
        //1.开启会话
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        //2.获取Mapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3.执行分页查询
        PageBean<Brand> pageBean = new PageBean<Brand>();
        //3.1查询某一页数据
        int beginIndex = (currentPage - 1) * pageSize;  //偏移量
        List<Brand> brandList = brandMapper.selectAllPage(beginIndex, pageSize); //所查的数据

        //3.2查询总数据条数
        int totalCount = brandMapper.selectCount();

        //3.3封装分页数据
        pageBean.setData(brandList);
        pageBean.setTotalCount(totalCount);
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        
        //计算总页数
        int totalPage = (int)(Math.ceil(totalCount*1.0/pageSize));
        pageBean.setTotalPage(totalPage);

        //4.sqlSession关闭
        sqlSession.close();
        //5.返回数据
        return pageBean;
    }

    //品牌查询
    public List<Brand> selectAll(){
        //1.开启会话
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        //2.获取Mapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3.执行查询
        List<Brand> brandList = brandMapper.selectAll();
        //4.sqlSession关闭
        sqlSession.close();
        //5.返回数据
        return brandList;
    }

    //品牌新增

    public void add(Brand brand) {
        //1.开启会话
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        //2.获取Mapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3.执行新增
        brandMapper.add(brand);
        //4、提交事务
        sqlSession.commit();
        //5、SqlSession关闭
        sqlSession.close();


    }
    //单个查询
    public Brand selectById(int id){
        //1.开启会话
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        //2.获取Mapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3.执行查询
        Brand brand = brandMapper.selectById(id);
        //4.sqlSession关闭
        sqlSession.close();
        //5.返回数据
        return brand;
    }

    //品牌修改

    public void update(Brand brand){
        //1.开启会话
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        //2.获取Mapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3.执行新增
        brandMapper.update(brand);
        //4、提交事务
        sqlSession.commit();
        //5、SqlSession关闭
        sqlSession.close();
    }

    //品牌删除
    public void delete(int id){
        //1.开启会话
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        //2.获取Mapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3.执行
        brandMapper.deleteById(id);
        //4、提交事务
        sqlSession.commit();
        //5、SqlSession关闭
        sqlSession.close();
    }


}